﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.CMS.Membership;
using System.Data.Linq.Mapping;
using MethodWorx.Core.DomainModel;
using System.Data.Linq;
using MethodWorx.CMS.Plugins;

namespace MethodWorx.CMS.Linq
{
    [Table(Name="dbo.User")]
    public class LinqUser : User, IPersistable
    {
        [Column(IsPrimaryKey = true, UpdateCheck=UpdateCheck.Never)]
        public new Guid Id { get { return base.Id; } set { base.Id = value; } }

        [Column(UpdateCheck = UpdateCheck.Never)]
        public new string Username
        {
            get { return base.Username; }
            set { base.Username = value; }
        }

        [Column(UpdateCheck = UpdateCheck.Never)]
        public new string Password
        {
            get { return base.Password;  }
            set { base.Password = value; }
        }

        [Column(UpdateCheck = UpdateCheck.Never)]
        public new bool Administrator
        {

            get { return base.Administrator;  }
            set { base.Administrator = value; }
        }

        [Column(UpdateCheck = UpdateCheck.Never)]
        public new string EmailAddress
        {

            get { return base.EmailAddress; }
            set { base.EmailAddress = value; }
        }

        private EntitySet<LinqUserRole> proxyUserRoles = new EntitySet<LinqUserRole>();
        [Association(Name = "UserRoles", OtherKey = "UserId", ThisKey = "Id", Storage = "proxyUserRoles")]
        public EntitySet<LinqUserRole> ProxyUserRoles
        {
            get { return this.proxyUserRoles; }
            set { this.proxyUserRoles.Assign(value); }
        }

        private EntitySet<LinqUserDashboardWidget> proxyDashboardWidgets = new EntitySet<LinqUserDashboardWidget>();
        [Association(Name = "UserDashboardWidgets", OtherKey = "UserId", ThisKey = "Id", Storage = "proxyDashboardWidgets")]
        public EntitySet<LinqUserDashboardWidget> ProxyDashboardWidgets
        {
            get { return this.proxyDashboardWidgets; }
            set { this.proxyDashboardWidgets.Assign(value); }
        }

        public override IEnumerable<Role> Roles
        {
            get
            {
                return this.ProxyUserRoles.Select(u => u.Role);
            }
        }

        public override IEnumerable<UserDashboardWidget> DashboardWidgets
        {
            get
            {
                return this.ProxyDashboardWidgets.Cast<UserDashboardWidget>();
            }
        }

        protected override void RemoveDashboardWidget(UserDashboardWidget dbw)
        {
            this.ProxyDashboardWidgets.Remove((LinqUserDashboardWidget)dbw);
            ((LinqUserDashboardWidget)dbw).User = null;
        }

        protected override void AddDashboardWidget(Plugins.IPlugin plugin, DashboardWidget result, int sequence, int columnIndex)
        {
            this.ProxyDashboardWidgets.Add(new LinqUserDashboardWidget(this, plugin, result, sequence, columnIndex));
        }

        #region Proxy Property For FirstName
        [Column(UpdateCheck=UpdateCheck.Never)]
        public new string FirstName { get { return base.FirstName; } set { base.FirstName = value; } }
        #endregion
        #region Proxy Property For LastName
        [Column(UpdateCheck=UpdateCheck.Never)]
        public new string LastName { get { return base.LastName; } set { base.LastName = value; } }
        #endregion
        #region Proxy Property For UserType
        [Column(UpdateCheck=UpdateCheck.Never)]
        public byte UserType { get { return (byte)base.UserType; } set { base.UserType = (UserType)value; } }
        #endregion     
        #region Proxy Property For Active
        [Column(UpdateCheck=UpdateCheck.Never)]
        public new bool Active { get { return base.Active; } set { base.Active = value; } }
        #endregion

        [Column(UpdateCheck=UpdateCheck.Never, Name="Attributes")]
        public string AttributesStorage { get; set; }

        private Dictionary<string, string> attributes;
        public override Dictionary<string, string> Attributes
        {
            get
            {
                if (this.attributes == null && this.AttributesStorage != null)
                {
                    this.attributes = (Dictionary<string, string>)
                                      this.AttributesStorage.Base64DeserializeUsingXml();
                }

                return this.attributes;
            }
            set 
            {
                this.AttributesStorage = value.Base64SerializeUsingXml();
                this.attributes = value; 
            }
        }
      
    }
}
